<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
<body>
	<!-- 登录 -->
	<script type="text/javascript">
		layui.use('layer',function() {
			$("#btn").click(function() {
				if (check()) {
					var userpwd1 = $("#userpwd").val();
					var username1 = $("#username").val();
					ajaxreg(username1, userpwd1);
				}
			});
			function ajaxreg(username1, userpwd1) {
				var cksremember = "no";
				if($("#cksremember").val() == "on"){
					cksremember = "ok";
				}
				$.ajax({
					url : "loginForward",
					type : "post",
					data : {
						username : username1,
						userpwd : userpwd1,
						cksremember : cksremember
					},
					success : function(data) {
						if (data.msg == "登录成功") {
							layer.msg(data.msg, {
								icon : 1,
								time : 1000
							});

							location.reload();
							return false;
						} else {
							layer.msg("登录失败", {
								icon : 2,
								time : 1000
							});
						}
					},
					dataType : "json"
				});
			}
			
			$("#username").blur(function(){
				var username = $("#username").val();
				if(username == ""){
					$("#tishi1").html("请输入用户名！");
					$("#tishi1").css("color", "red");
				}else{
					$("#tishi1").html("");
				}
			})
			
			$("#userpwd").blur(function(){
				var userpwd = $("#userpwd").val();
				if(userpwd == ""){
					$("#tishi2").html("请输入密码！");
					$("#tishi2").css("color", "red");
				}else{
					$("#tishi2").html("");
				}
			})
			
			
			function check() {
				var username = $("#username").val();
				var userpwd = $("#userpwd").val();
				if (username == "") {
					return false;
				} else if (userpwd == "") {
					return false;
				}
				return true;
			}
		});
	</script>

	<!-- 注册 -->
	<!--密码-->
	<script>
	layui.use('layer', function() {
		//用户名格式校验
		$("#username1").blur(function(){
			var regExp1 = /^[\u4e00-\u9fa5_a-zA-Z]*$/;
			var uname1 = $("#username1").val();
			if(regExp1.test(uname1)){
				$("#tishi3").html("");
			}else{
				$("#tishi3").html("用户名只包括英文字母与汉字!");
				$("#tishi3").css("color", "red");
			}
			
		})
		//电话校验
		$("#usertel1").blur(function(){
			var regExp2 = /^1[34578]\d{9}$/;
			var utel = $("#usertel1").val();
			if(regExp2.test(utel)){
				$("#tishi4").html("");
			}else{
				$("#tishi4").html("电话号码格式错误!");
				$("#tishi4").css("color", "red");
			}
		})

		//密码格式校验
		$("#userpwd1").blur(function(){
			var regExp3 = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
			var upwd = $("#userpwd1").val();
			if(regExp3.test(upwd)){
				$("#tishi5").html("");
			}else{
				$("#tishi5").html("密码必须包含字母和数字，且在6~18位之间!");
				$("#tishi5").css("color", "red");
			}
		})
		//检查两次密码是否相同
		$("#userpwds1").blur(function() {
			if ($("#userpwd1").val() != "" && $("#userpwds1").val() != "") {
				var pwd = $("#userpwd1").val();
				var pwd1 = $("#userpwds1").val();
				if (pwd != pwd1) {
					$("#tishi6").html("两次密码不同!");
					$("#tishi6").css("color", "red");
				}else{
					$("#tishi6").html("");
				}
			}
		});
		$("#submitt").click(function() {
			if (check()) {
				var userpwd1= $("#userpwd1").val();
				var username1 = $("#username1").val();
				ajaxreg(username1, userpwd1);
			}
		});
		function ajaxreg(username1, userpwd1) {
			var usertel1 = $("#usertel1").val();
			console.log(username1,userpwd1,usertel1)
			 $.ajax({
				url : "reg",
				type : "post",
				data : JSON.stringify({
					username:username1,
					usertel:usertel1,
					userpwd:userpwd1
					}),
					contentType: "application/json",
					success : function(data) {
						if (data.code == 1) {
							layer.msg("用户名已存在", {
								icon : 1,
								time : 1000
							});
							return false;
						}else if(data.code == 4){
							layer.msg("电话号已存在", {
								icon : 1,
								time : 1000
							});
							return false;
						} else if(data.code == 2){
							layer.msg(data.msg, {
								icon : 1,
								time : 1000
							});
							//关闭修改密码的页面
							$('.overlay1').removeClass('open').addClass('close');
							setTimeout(function(){
							$('.overlay1').removeClass('close');}, 500);
							//打开登录的页面	 
					        $('.overlay').removeClass('close').addClass('open');
						} else if(data.code == 0){
							layer.msg("添加失败", {
								icon : 1,
								time : 1000
							});
						}
					},
					dataType : "json"
			 }); 
		}
		
		function check() {
			var username = $("#username1").val();
			var userpwd = $("#userpwd1").val();
			var userpwds = $("#userpwds1").val();
			var usertel = $("#usertel1").val();
			if (username == "") {
				$("#tishi1").html("请输入用户名！");
				$("#tishi1").css("color", "red");
				return false;
			} else if (usertel == "") {
				$("#tishi2").html("请输入电话！");
				$("#tishi2").css("color", "red");
				return false;
			} else if ($("#tishi6").html() == "两次密码不同!") {
				return false;
			} else if($("#tishi3").html() == "用户名只包括英文字母与汉字!"){
				return false;
			} else if($("#tishi5").html() == "密码必须包含字母和数字，且在6~18位之间!"){
				return false;
			} else if($("#tishi4").html() == "电话号码格式错误!"){
				return false;
			}
			return true;
		}
	});
	</script>
	
	<script>
	 $(function(){
		  //("#username").blur 文本框失去焦点事件
	  		$("#username").blur(function(){
			  //读取用户名和密码的cookie信息
			  var username = $.cookie('username');
			  var userpwd = $.cookie('userpwd');
			  //如果用户输入的用户名 和 读取用户名一致
			  if($(this).val() == username){
			     //就将密码显示在密码框中
			     $("#userpwd").val(userpwd); 
			  }
		 });
	 })
	</script>
	
	<script type="text/javascript">
		function logout(){
			$.ajax({
				type : "get",
				url : "logout",
				data : {},
				success : function(data){
					if(data.code == 200){
						$.session.remove("userpic");
						$.session.remove("username");
						location.reload();
					}
				}
			})
		}
	</script>
	
	
	<!-- 找回密码 -->
	<script>
		function captcha(e) {
			clickTime(e);
			var href = "{:U('Login/setCode')}";
			var tel = $('input[name="member_list_tel"]').val();
			var usertel = $("#tel").val();
			console.log(usertel);
			//ajax获取数据
			$.ajax({
				url : "sendApi/" + usertel,
				type : "post",
				/*  data:{tel:tel}, */
				success : function(data) {
					if (data.msg == "手机号不存在") {
						alert("手机号不存在");
						return false;
					} else if (data.msg == "验证码发送成功") {
						alert("验证码发送成功");
						return false;
					}
				},
			});
		}
	
		var wait = 60;
		function clickTime(e) {
			var e = $(e);
			if (wait == 0) {
				e.onClick = captcha();
				e.html("获取验证码");
				e.removeClass(' layui-btn-disabled');
				wait = 60;
			} else {
				e.unbind();
				e.html("重新发送(" + wait + ")");
				e.addClass(' layui-btn-disabled');
				wait--;
				setTimeout(function() {
					clickTime(e)
				}, 1000)
			}
		}
		
	//点击获取验证码时，校验手机号码非空	
	layui.use('layer',function(){
		var layer = layui.layer;
		$('#ver').click(function() {
			var tel=$("#tel").val(); 
			console.log("手机号："+tel);
			if(tel ==""){
				layer.msg("请输入手机号", {
					icon : 7,
					time : 1000
				});			
			}else{
				captcha(this);
			}
		});
	});
	
	//手机验证码校验
	layui.use('layer',function(){
		var layer = layui.layer;
		$("#btn1").click(function() {
		    var tel=$("#tel").val();
			var yzm = $("#yzm").val();
			console.log("手机号："+tel+",验证码："+yzm);
			if(tel ==""){
				layer.msg("请输入手机号", {
					icon : 7,
					time : 1000
				});	
			}else if(yzm==""){
				layer.msg("请输入验证码", {
					icon : 7,
					time : 1000
				});	
			}else{ 
				//校验验证码是否正确
				$.ajax({
					url : "getApi/"+yzm, 
					type : "post",
					success : function(data) {
						if (data.code == 200) {
							
						   //关闭找回密码的页面
				           $('.overlay2').removeClass('open').addClass('close');
				           setTimeout(function(){
				           $('.overlay2').removeClass('close');}, 500); 
				           //打开修改密码的页面		
			               $('.overlay3').removeClass('close').addClass('open');
				           
						   return false;
						} else {
							layer.msg("验证码无效或错误，请重新输入！", {
								icon : 7,
								time : 1000
							});	
							return false;
					    }
					},
				}); 	
		    }  
	     });
	});	
	</script>

	<!-- 修改密码 -->
	<script>
		layui.use('upload', function() {
			//检查两次密码是否相同
			$("#updpwds").blur(function() {
				if ($("#updpwd").val() != "" && $("#updpwds").val() != "") {
					var pwd = $("#pwd").val();
					var pwd1 = $("#pwds").val();
					if (pwd != pwd1) {
						$("#tishi").html("两次密码不同!");
						$("#tishi").css("color", "red");
					} else {
						$("#tishi").html("两次密码相同!");
						$("#tishi").css("color", "red");
					}
				}
			});
			$("#updsubmitt").click(function() {
				if (check()) {
					var userpwd1 = $("#updpwd").val();
					ajaxreg(userpwd1);

				}
			});
			function ajaxreg(userpwd1) {
				$.ajax({
					url: "updPwd/" + userpwd1,
					type: "post",
					contentType: "application/json",
					success: function(data) {
						if (data.code == 200) {
							layer.msg("修改成功！", {
								icon: 1,
								time: 1000
							});
							//关闭修改密码的页面
							$('.overlay3').removeClass('open').addClass('close');
							setTimeout(function(){
							     $('.overlay3').removeClass('close');}, 500);
							//打开登录的页面	 
					        $('.overlay').removeClass('close').addClass('open');

						} else if (data.code == 500) {
							layer.msg("修改失败", {
								icon: 1,
								time: 1000
							});
						}
					},
					dataType: "json"
				});
			}

			function check() {
				var updpwd = $("#updpwd").val();
				var updpwds = $("#updpwd").val();
				if (updpwd == "" && updpwds == "") {
					$("#tishi").html("请输入密码！");
					$("#tishi").css("color", "red");
					return false;
				} else if ($("#tishi").html() == "两次密码不同!") {
					return false;
				}
				return true;
			}
		});
	</script>
</body>
</html>